package com.atguigu.lease.web.admin.service.impl;
import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private ProvinceInfoMapper provinceInfoMapper;
    @Autowired
    private CityInfoMapper cityInfoMapper;
    @Autowired
    private DistrictInfoMapper districtInfoMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;

    @Autowired
    private ApartmentLabelService apartmentLabelService;

    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private FeeValueMapper feeValueMapper;

    @Override
    public void insertApartment(ApartmentSubmitVo apartmentSubmitVo) {
        //1.根据省市县查询名称
        ProvinceInfo provinceInfo = provinceInfoMapper.selectById(apartmentSubmitVo.getProvinceId());
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());

        CityInfo cityInfo = cityInfoMapper.selectById(apartmentSubmitVo.getCityId());
        apartmentSubmitVo.setCityName(cityInfo.getName());

        DistrictInfo districtInfo = districtInfoMapper.selectById(apartmentSubmitVo.getDistrictId());
        apartmentSubmitVo.setDistrictName(districtInfo.getName());

        //2.完成公寓入库，并自主实现主键回显
        this.saveOrUpdate(apartmentSubmitVo);
        //3.获取公寓ID号
        Long apartmentId = apartmentSubmitVo.getId();

        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        ArrayList<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
        graphVoList.forEach(GraphVo->{
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setName(GraphVo.getName());
            graphInfo.setUrl(GraphVo.getUrl());
            graphInfo.setItemId(apartmentId);
            graphInfo.setItemType(ItemType.APARTMENT);
            graphInfoList.add(graphInfo);
        });
        graphInfoService.saveBatch(graphInfoList);

        //5.完成公寓配套入库
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        List<ApartmentFacility> apartmentFacilityList = new ArrayList<>(facilityInfoIds.size());
        facilityInfoIds.forEach(facilityInfoId -> {
            ApartmentFacility apartmentFacility =
                    ApartmentFacility.builder()
                            .facilityId(facilityInfoId)
                            .apartmentId(apartmentId).build();
            apartmentFacilityList.add(apartmentFacility);
        });
        apartmentFacilityService.saveBatch(apartmentFacilityList);

        //6.完成公寓标签入库
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        List<ApartmentLabel> apartmentLabelList = new ArrayList<>(labelIds.size());
        labelIds.forEach(labelId -> {
            ApartmentLabel apartmentLabel =
                    ApartmentLabel.builder().labelId(labelId).apartmentId(apartmentId).build();
            apartmentLabelList.add(apartmentLabel);
        });
        apartmentLabelService.saveBatch(apartmentLabelList);

        //7.杂费入库
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        List<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>(feeValueIds.size());
        feeValueIds.forEach(feeValueId -> {
            ApartmentFeeValue apartmentFeeValue =
                    ApartmentFeeValue.builder().feeValueId(feeValueId).apartmentId(apartmentId).build();
            apartmentFeeValueList.add(apartmentFeeValue);
        });
        apartmentFeeValueService.saveBatch(apartmentFeeValueList);
    }

    @Override
    @Transactional
    public void updateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        //1.先删除数据
        deleteApartmentAssociatedById(apartmentSubmitVo.getId());

        //2.之后更新/新增
        insertApartment(apartmentSubmitVo);
    }

    @Override
    @Transactional
    public void deletedApartmentById(Long id) {
        //1.判断公寓中是否有房间信息  房间信息为0 可以删除
        LambdaQueryWrapper<RoomInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RoomInfo::getApartmentId,id);
        long count = roomInfoMapper.selectCount(lambdaQueryWrapper);

        if(count > 0){
            throw new LeaseException(ResultCodeEnum.APARTMENT_HASROOM_ERROR);
        }

        //2.删除公寓和关联字段数据
        deleteApartmentAssociatedById(id);


        //删除公寓
        apartmentInfoMapper.deleteById(id);
    }

    @Override
    public void deleteApartmentAssociatedById(Long id) {
        //删除图片
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,id));

        //删除配套信息
        apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,id));

        //删除标签
        apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,id));

        //删除杂费
        apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,id));
    }

    @Override
    public IPage<ApartmentItemVo> pageItem(long current, long size, ApartmentQueryVo queryVo) {
        IPage<ApartmentItemVo> page = new Page(current,size);
        return apartmentInfoMapper.pageItem(page,queryVo);
    }

    @Override
    public ApartmentDetailVo getDetailById(Long apartmentId) {
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(apartmentId);
        //为什么和null比较
        if(apartmentInfo == null){
            throw new LeaseException(ResultCodeEnum.APARTMENT_ERROR);
        }
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);

        List<GraphVo> graphVoList = graphInfoMapper.findGraphListByItemId(ItemType.APARTMENT,apartmentId);
        List<LabelInfo> labelInfoList = labelInfoMapper.findListByApartmentId(apartmentId);
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.findListByApartmentId(apartmentId);
        List<FeeValueVo> feeValueVoList = feeValueMapper.findListByApartmentId(apartmentId);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return apartmentDetailVo;
    }
}




